package com.cityguardian.cityguardianapp;

import com.cityguardian.model.Alerta;
import com.cityguardian.model.Usuario;
import com.cityguardian.threads.AsyncTaskFoursquare;
import com.google.android.gms.maps.model.LatLng;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;

public class SearchActivity extends ActionBarActivity {

	private Usuario usuario;
	//private Usuario tercero;
	private Alerta alerta;
	private LatLng posicion;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);
		// Show the Up button in the action bar.
		setupActionBar();
		Intent intent = getIntent();
		usuario = (Usuario) intent.getSerializableExtra("Usuario");
		alerta = (Alerta) intent.getSerializableExtra("Alerta");
		//tercero = (Usuario) intent.getSerializableExtra("Tercero");
		posicion = (LatLng) intent.getParcelableExtra("Posicion");
		
	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		} else {
		    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.search, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case android.R.id.home:
				NavUtils.navigateUpFromSameTask(this);
				return true;
            case R.id.action_button:
                sendUserForFillActivity();
                return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	private void sendUserForFillActivity() {
		Intent intent = new Intent(this, FillAlertActivity.class);
		intent.putExtra("Usuario", this.usuario);
		if (posicion!=null){
			intent.putExtra("Latitud", posicion.latitude);
			intent.putExtra("Longitud", posicion.longitude);
		}else{
			intent.putExtra("Latitud", Double.parseDouble(alerta.getPosicionY()));
			intent.putExtra("Longitud", Double.parseDouble(alerta.getPosicionX()));
		}
    	startActivity(intent);
	}
	
	public void showAlertList() {
		Intent intent = new Intent(this, AlertListActivity.class);
		startActivity(intent);
	}
	
	public void go_hospital(View view){
		LatLng lastPosition;
		if (posicion!=null){
			lastPosition = posicion;
		}else{
			lastPosition = new LatLng(Double.valueOf(alerta.getPosicionY()), Double.valueOf(alerta.getPosicionX()));
		}
		AsyncTaskFoursquare thread = new AsyncTaskFoursquare(this,lastPosition,"Hospital",usuario);
		thread.execute();
		
	}
	
	public void go_comisaria(View view){
		LatLng lastPosition;
		if (posicion!=null){
			lastPosition = posicion;
		}else{
			lastPosition = new LatLng(Double.valueOf(alerta.getPosicionY()), Double.valueOf(alerta.getPosicionX()));
		}
		AsyncTaskFoursquare thread = new AsyncTaskFoursquare(this,lastPosition,"Comisaria",usuario);
		thread.execute();		
	}
	
	public void go_bomberos(View view){
		LatLng lastPosition;
		if (posicion!=null){
			lastPosition = posicion;
		}else{
			lastPosition = new LatLng(Double.valueOf(alerta.getPosicionY()), Double.valueOf(alerta.getPosicionX()));
		}
		AsyncTaskFoursquare thread = new AsyncTaskFoursquare(this,lastPosition,"Bomberos",usuario);
		thread.execute();		
	}
	
	public void go_municipalidad(View view){
		LatLng lastPosition;
		if (posicion!=null){
			lastPosition = posicion;
		}else{
			lastPosition = new LatLng(Double.valueOf(alerta.getPosicionY()), Double.valueOf(alerta.getPosicionX()));
		}
		AsyncTaskFoursquare thread = new AsyncTaskFoursquare(this,lastPosition,"Municipalidad",usuario);
		thread.execute();		
	}
	
	public void go_clinica(View view){
		LatLng lastPosition;
		if (posicion!=null){
			lastPosition = posicion;
		}else{
			lastPosition = new LatLng(Double.valueOf(alerta.getPosicionY()), Double.valueOf(alerta.getPosicionX()));
		}
		AsyncTaskFoursquare thread = new AsyncTaskFoursquare(this,lastPosition,"Clinica",usuario);
		thread.execute();		
	}
}
